<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<%@page import="java.util.*"%>
<%@page import="support.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Choose Universities Page</title>
<script type=text/javascript>
	function GetXmlHttpObject() {
		var xmlHttp = null;
		try {
			// Firefox, Opera 8.0+, Safari
			xmlHttp = new XMLHttpRequest();
		} catch (e) {
			// Internet Explorer
			try {
				xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
		}
		return xmlHttp;
	}

	function validate() {

		var xmlHttp = null;
		xmlHttp = GetXmlHttpObject();

		if (xmlHttp == null) {
			alert("Your browser does not support AJAX!");
		}

		var url = "university-ajax.jsp";
		var uni = document.getElementById("uni").value;
		url = url + "?university=" + uni;
		xmlHttp.open("GET", url, true);
		xmlHttp.send(null);

		xmlHttp.onreadystatechange = function() {
			if (xmlHttp.readyState == 4) {
				var xmlDoc = document.getElementById("fxm");
				xmlDoc.innerHTML = xmlHttp.responseText;
				var s = xmlHttp.responseText;
				if (s != "Already exists!") {
					add = "degreeDiscipline.jsp?university=" + uni;
					window.location = add;
				}
			}
		};

		return false;
	}
</script>


</head>
<body>
	<%
		//get the stateid of chosen
		String stateName = request.getParameter("stateName");
	%>
	<%
		support s = new support();

		//getUniversities returns a vector of vectors where each vector is a tuple of <string, vector>
		//with the string being the name of the country/state and the vector being the list of universities there
		Vector universities = s.getUniversities(stateName);
		//get vector of universities of chosen state
		session.setAttribute("location", stateName);
	%>
	<p>
		Your location:
		<%=stateName%>
	<table border="1">
		<%
			//display all the universities of chosen state
			//in 3 columns
			for (int i = 0; i < universities.size() / 3; i++) {
				String univ = (String) universities.get(i * 3);
		%>
		<tr>
			<td><a href="degreeDiscipline.jsp?university=<%=univ%>"> <%
 	out.println("<br>" + univ + "<br>");
 %>
			</a></td>
			<%
				String univ2 = (String) universities.get(i * 3 + 1);
			%>
			<td><a href="degreeDiscipline.jsp?university=<%=univ2%>"> <%
 	out.println("<br>" + univ2 + "<br>");
 %></a></td>
			<%
				String univ3 = (String) universities.get(i * 3 + 2);
			%>
			<td><a href="degreeDiscipline.jsp?university=<%=univ3%>"> <%
 	out.println("<br>" + univ3 + "<br>");
 	}
 %>
			</a></td>
		</tr>
		<%
			//print the last row of states
			if (universities.size() % 3 != 0) {
				for (int i = 0; i < universities.size() % 3; i++) {
					String univ4 = (String) universities.get(universities
							.size() - 1 - i);
		%>
		<tr>
			<td><a href="degreeDiscipline.jsp?university= <%=univ4%>"> <%
 	out.println(univ4);
 %>
			</a></td>
		</tr>
		<%
			}
			}
		%>
	</table>
	<p>
		Cannot find your university? <br>
	<p>Type your university name here:
	<form method="get" action="degreeDiscipline.jsp"
		onsubmit="return validate()">
		<input id="uni" type="text" size="30" name="university"> <input
			type="submit" size="10" value="submit">
	</form>
</body>

<div id='fxm'></div>

</html>